import { WeaDialog, WeaTab, WeaRightMenu, WeaNewScroll, WeaAlertPage } from 'ecCom';
import { Button, Modal, message, Spin } from 'antd';
import { inject, observer } from 'mobx-react';
import ContractInfo from './ContractInfo';
import ContractDiscuss from './ContractDiscuss';
import { toJS } from 'mobx';
import ContractShare from './ContractShare';
import equal from 'deep-equal';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

@observer
export default class ContractOperate extends React.Component {
  componentWillUnmount() {

  }

  componentDidMount() {
    const { contentStore } = this.props;
    contentStore.componentDidMount(this);
  }
  render() {
    const { contentStore } = this.props;
    const { dialogPageType, modleSelectTabKey, isShowDialog, hasTab, exchangeList, shareStore, shareList, contractId, canEdit, cardloading, contractShareCondition } = contentStore;
    const modleTabDatas = [];
    modleTabDatas.push({ title: getLabel(1361, '基本信息'), key: '1' });
    modleTabDatas.push({ title: getLabel(15153, '相关交流'), key: '2' });
    // modleTabDatas.push({ title: getLabel(2112, '共享设置'), key: "3" });
    if (contractId && canEdit) {
      modleTabDatas.push({ title: getLabel(2112, '共享设置'), key: '3' });
    }

    return (

      <WeaDialog
        ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@kq4tot`}
        title={(dialogPageType == 'add' ? getLabel(82, '新建') : getLabel(367, '查看')) + getLabel(34244, '合同信息')}
        icon="icon-coms-crm"
        iconBgcolor="#96358a"
        visible={isShowDialog}
        style={{ width: 1050, height: 600 }}
        onCancel={this.closeDialog.bind(this)}
        buttons={this.getDialogBottomBtns()}
      >
        <WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@084kap`} height="100%">
          {
            hasTab &&
            <WeaTab
              ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@q3azcs`}
              selectedKey={modleSelectTabKey}
              keyParam="key" // 主键
              countParam="groupid" // 数量
              onChange={this.modalChangeData.bind(this)}
              datas={modleTabDatas}
            />
          }

          {
            modleSelectTabKey == '1' &&
            <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@cl7hho`} datas={this.getDialogRightMenu()} onClick={this.onDialogRightMenuClick.bind(this)}>
              <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@59vk8z`} spinning={cardloading} >
                <ContractInfo ecId={`${this && this.props && this.props.ecId || ''}_ContractInfo@uqfior`} contentStore={contentStore} />
              </Spin>
            </WeaRightMenu>
          }
          {
            modleSelectTabKey == '2' &&
            <ContractDiscuss ecId={`${this && this.props && this.props.ecId || ''}_ContractDiscuss@8p4keb`} contentStore={contentStore} datas={toJS(exchangeList)} />
          }
          {

            modleSelectTabKey == '3' &&
            <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@novppw`} datas={this.contractShareRightMenu()} onClick={this.contractShareRightMenuClick.bind(this)} >
              <ContractShare ecId={`${this && this.props && this.props.ecId || ''}_ContractShare@oabuku`} ref="contractShare" condition={toJS(contractShareCondition)} data={toJS(shareList)} saveCurrentDatas={this.saveCurrentDatas} />
            </WeaRightMenu>
          }
        </WeaNewScroll>

      </WeaDialog>

    );
  }
  getDialogBottomBtns() {
    const { contentStore, location } = this.props;
    const { status, contractId, canEdit, dialogPageType, modleSelectTabKey, editInfo, saveInfo, cardloading } = contentStore;
    const btns = [];
    if (modleSelectTabKey == '1') {
      if (dialogPageType == 'view') {
        if (canEdit && status == '0') {
          btns.push((<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@cl7cun@edit`} type="primary" onClick={() => { editInfo(contractId); }}>{getLabel(93, '编辑')}</Button>));
        }
      } else if (dialogPageType == 'edit') {
        if (canEdit && status == '0') {
          btns.push((<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@gl1lva@save`} type="primary" disabled={cardloading} onClick={() => { saveInfo(contractId); }}>{getLabel(86, '保存')}</Button>));
        }
      }
    }
    { modleSelectTabKey == '3' && btns.push((<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@i2wid4@save`} type="primary" disabled={cardloading} onClick={() => { this.contractShareRightMenuClick('0'); }}>{getLabel(86, '保存')}</Button>)); }
    { dialogPageType == 'add' && modleSelectTabKey == '1' && btns.push((<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@pv4gb1@save`} type="primary" disabled={cardloading} onClick={() => { saveInfo('', location.query.customerId); }}>{getLabel(86, '保存')}</Button>)); } // 卡片新建

    btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5str21@close`} type="ghost" onClick={this.closeDialog.bind(this)}>{getLabel(309, '关闭')}</Button>);
    return btns;
  }
  closeDialog() {
    const { contentStore } = this.props;
    contentStore.closeContractDialog();
  }
  modalChangeData(key) {
    const { changeContractModalTab, contractId, status, getContractShareList, getExchangeList, getContractInfo, getFormCondition } = this.props.contentStore;
    changeContractModalTab(key);
    if (key == '3') { getContractShareList({ contractId }, '3', true); getFormCondition(); }
    if (key == '2') { getExchangeList({ contractId }, '2', true); }
    if (key == '1') { getContractInfo({ contractId, operation: status == '2' ? 'edit' : 'view' }, '1', true); }
  }
  getDialogRightMenu() {
    const { rightMenuInfo, cardloading } = this.props.contentStore;
    const items = [];
    rightMenuInfo.map((item, index) => {
      items.push({
        icon: <i className={item.icon} />,
        content: item.content,
        disabled: cardloading,
        key: item.key,
      });
    });
    return items;
  }
  onDialogRightMenuClick(key) {
    const { location } = this.props;
    const { contractId, hasTab, sign, complete, approveInfo, statusBack, editInfo, getContractInfo, saveInfo, getContractShareList, deleteContract, getFormCondition } = this.props.contentStore;
    if (key == '2') { // 签单
      sign(contractId, 'rightmenu');
    }
    if (key == '3') { // 执行完成
      complete(contractId, 'rightmenu');
    }
    if (key == '8') { // 重新打开
      statusBack(contractId, 'rightmenu');
    }
    if (key == '6') { // 共享设置
      getContractShareList({ contractId }, '3', true);
      getFormCondition();
    }
    if (key == '0') { // 编辑
      editInfo(contractId);
    }
    if (key == '1') { // 提交审批
      approveInfo(contractId, 'rightmenu');
    }
    if (key == '7') { // 保存
      saveInfo(contractId, location.query.customerId || '');
    }
    if (key == '9') { // 返回
      getContractInfo({ contractId, operation: 'view' }, '1', hasTab);
    }
    if (key == '10') { // 删除
      Modal.confirm({
        title: getLabel(15172, '系统提示'),
        content: getLabel(83600, '您确认要删除吗？'),
        okText: getLabel(33703, '确定'),
        cancelText: getLabel(201, '取消'),
        onOk() {
          deleteContract(contractId, 'rightmenu');
        },
        onCancel() { },
      });
    }
  }
  contractShareRightMenu() {
    const btns = [];
    btns.push({
      icon: <i className="icon-coms-Preservation" />,
      content: getLabel(86, '保存'),
    });
    return btns;
  }
    // 共享设置数据保存
    saveCurrentDatas = (data) => {
      const { updateShareList } = this.props.contentStore;
      updateShareList(data);
    }
    contractShareRightMenuClick(key) {
      const { shareList, shareListNew, contractId, contractShareSave } = this.props.contentStore;
      const _shareData = toJS(shareListNew);
      const _shareList = toJS(shareList);
      const addshareList = [];
      let oldArr = (function (arr) {
        const idArr = [];
        for (let i = 0; i < arr.length; i++) {
          idArr.push(arr[i].dataId);
        }
        return idArr;
      }(_shareList));
      const lastArr = [];
      if (key == '0') {
        if (!equal(_shareData, _shareList)) {
          for (let i = 0; i < _shareData.length; i++) {
            if (!_shareData[i].dataId) {
              addshareList.push(_shareData[i]);
            } else {
              lastArr.push(_shareData[i].dataId);
            }
          }
          lastArr.map((key) => {
            oldArr = oldArr.filter(data => data !== key);
          });
          contractShareSave({ contractId, deleteIds: `${oldArr}`, shareList: JSON.stringify(addshareList) });
        } else {
          message.error(getLabel(384175, '请先添加权限再保存'));
        }
      }
    }
}
